package com.hctforgreen.greenservice.model;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 考试试卷
 * 
 * @author denghb
 * 
 */
public class ExamPaperEntity extends BaseEntity {
	/**
	 * 
	 */
	private static final long serialVersionUID = 0x1921L;
	private static ExamPaperEntity examPaperEntity = new ExamPaperEntity();

	public static ExamPaperEntity getInstance(){
		return examPaperEntity;
	}
	public String id;// "45812fbc-0dc6-11e5-bbe6-2c27d7419219; 试卷id
	public String createTime;// 发布时间
	public String moduleId;// 板块id
	public String remark;// 备注
	public String lastUpdateTime;
	public String name;// 试题
	public Integer score;// 总分
	public Integer passScore;// 及格分
	public String seriesId;// 系列id
	public String creator;// 创建者
	public List<BookClass> bookList;

	//一次考题有可能包含多份资料
	public class BookClass implements Serializable{
	 public String bookId;
	 public String bookName;

		public BookClass(){
			super();
		}
		public String getBookId() {
			return bookId;
		}

		public void setBookId(String bookId) {
			this.bookId = bookId;
		}

		public String getBookName() {
			return bookName;
		}

		public void setBookName(String bookName) {
			this.bookName = bookName;
		}
	}
	public int isPass;// 是否通过考试 0：未通过考试 1：通过考试

	public String personId;

	public PAPER_TYPE paperType = PAPER_TYPE.TYPE_NET;

	/**
	 * 试卷类型
	 * 
	 * @author dhb
	 * 
	 */
	public enum PAPER_TYPE {
		TYPE_NET, // 记录或者未考试卷
		TYPE_CACHE// 草稿
	}

	/**
	 * json解析
	 * 
	 * @param jsonObject
	 * @return ExamPaperEntity entity
	 * @throws Exception
	 */
	public  ExamPaperEntity parse(JSONObject jsonObject) throws Exception {
		ExamPaperEntity entity = new ExamPaperEntity();
		if (jsonObject == null) {
			return entity;
		}
		if (jsonObject.has("id")) {
			entity.id = jsonObject.getString("id");
		}
		if (jsonObject.has("createTime")) {
			entity.createTime = jsonObject.getString("createTime");
		}
		if (jsonObject.has("creator")) {
			entity.creator = jsonObject.getString("creator");
		}
		if (jsonObject.has("moduleId")) {
			entity.moduleId = jsonObject.getString("moduleId");
		}
		if (jsonObject.has("name")) {
			entity.name = jsonObject.getString("name");
		}
		if (jsonObject.has("passScore")) {
			entity.passScore = jsonObject.getInt("passScore");
		}
		if (jsonObject.has("remark")) {
			entity.remark = jsonObject.getString("remark");
		}
		if (jsonObject.has("score")) {
			entity.score = jsonObject.getInt("score");
		}
		if (jsonObject.has("seriesId")) {
			entity.seriesId = jsonObject.getString("seriesId");
		}
		if (jsonObject.has("isPass")) {
			entity.isPass = jsonObject.getInt("isPass");
		}
		if (jsonObject.has("bookList")){
			entity.bookList = new ArrayList<BookClass>();
			JSONArray jsonArray = jsonObject.getJSONArray("bookList");
			if (jsonArray != null && jsonArray.length() > 0){

				for (int i=0;i<jsonArray.length();i++){
					JSONObject obj = jsonArray.getJSONObject(i);
					 BookClass bookClass = new BookClass();
					if (obj.has("bookId")) {
						bookClass.setBookId(obj.getString("bookId"));
					}
					if (obj.has("bookName")){
						bookClass.setBookName(obj.getString("bookName"));
					}
					entity.bookList.add(i,bookClass);
				}
			}
		}


		entity.paperType = PAPER_TYPE.TYPE_NET;
		return entity;
	}

	@Override
	public String toString() {
		return "ExamPaperEntity{" +
				"id='" + id + '\'' +
				", createTime='" + createTime + '\'' +
				", moduleId='" + moduleId + '\'' +
				", remark='" + remark + '\'' +
				", lastUpdateTime='" + lastUpdateTime + '\'' +
				", name='" + name + '\'' +
				", score=" + score +
				", passScore=" + passScore +
				", seriesId='" + seriesId + '\'' +
				", creator='" + creator + '\'' +
				", bookList=" + bookList +
				", isPass=" + isPass +
				", personId='" + personId + '\'' +
				", paperType=" + paperType +
				'}';
	}
}